/**
 * Information agent: display an information to its receivers
 * 
 * can be used to inform a group of people the meeting date
 *or to inform a person that you will be late
 *
 *@author Francis,Aidan
 */
 public class InfoAgent implements Agent {
       private String message; //a message
       
	   private Vector contacts;    //storage for the names of contacts (string)
   	   	private Vector addresses;   // store the receivers' numbers
    
	//private int Numreceivers;// number of receivers
	private char connectionType; //decide on datagram or socket mode of sending agent 
	private String agentType;
   //constructor
    public InfoAgent()
	{
	 	contacts = new Vector();
		addresses = new Vector();
	}
	/**
	*give the agent the information to be sent
	*@Param message(informaton)
	*/
	public void setMessage(String m)
	{
	  message=m;
	}
    /**
	*return the information you want the agent to transfer
	*/
    public String getMessage()
	{
		
		return message;
	}
	
  
/**
*access a contact details from the storage vector
*based on an index
*
*@param n=index
*/
public String getContact(int n)
{
return this.contacts.elementAt(n).toString();
	
}

public void setType()
{
	this.agentType=Agent.TYPE_INFORMATION;
	
}

/**
*return the index of the next recipient
*/
public int getNextIndex(){
		for(int i = 0 ; i < contacts.size();i++){
			if(available[i][0]==false){
				return i;
			}
		}
		return -1;
	}
/**
*set the connection mode to dataGram connection
*/
public void setConnectionDatagram(){
		this.connectionType = TYPE_DATAGRAM;
	}
/**
*set the connection mode to socket connection
*/
public void setConnectionSocket(){
		this.connectionType = TYPE_SOCKET;
	}

	
	/**
	 * read in all the agents values from a string
	 * the order in which they are read in is important and should 
	 * be matched with the fromString method
	 * 
	 * @param agentString
	 */
	public void fromString( String agentString )
	{
		int i = 0;
		int j = 0;
		String field, value;
		Tokenizer tokField = new Tokenizer(agentString, FIELD_DELIM);

		try{
				this.agentType= tokField.nextToken();
		catch(NOsuchElementException e)
		{
			e.printStackTrace()
		}
		
		while( tokField.hasMoreTokens() ){			
			try {
				field = tokField.nextToken();
				value = field.substring(2);
				
				if( field.charAt(0) == FIELD_NAME ){					
					this.contacts.addElement(value);
				}
				else if( field.charAt(0) == FIELD_ADDRESS ){
					this.addresses.addElement(value);
				}
				else if( field.charAt(0) == FIELD_START ){
					this.startTimes.addElement(value);
				}
				else if( field.charAt(0) == FIELD_FINISH ){
					this.finishTimes.addElement(value);
				}
				else if( field.charAt(0) == ELEMENT_BOOL ){
					//if the boolean array has not been created, do so now
					if( available == null )
						this.setAvailability();					
					
					if( i < startTimes.size() ){
						if( j < contacts.size()+1 ){
							if( value.charAt(0) == 'T' )
								this.available[i][j] = true;
							else
								this.available[i][j] = false;
							
							j++;
						}
						else
							j = 0;
							i++;
					}
				}				
			} catch (NoSuchElementException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * the toString method writes out all the agents values 
	 * the order in which they are written out is important and should 
	 * be matched with the fromString method
	 * 
	 * @return the agent in string form
	 **/
	public String toString()
	{
		//first write out all contact names and addresses
		for( int i=0; i<this.contacts.size(); i++ ){
			agentString += FIELD_NAME;		//	N
			agentString += VALUE_DELIM;		//	=			
			agentString += this.contacts.elementAt(i); //name of the contact
			agentString += FIELD_DELIM;		//	|
			
			agentString += FIELD_ADDRESS;	//	A
			agentString += VALUE_DELIM;		//	=
			agentString += this.addresses.elementAt(i);//address
			agentString += FIELD_DELIM;		//	|
		}
		return agentString;
	}
	
	
	
	/**
	 * @return address is the address agent is to be sent to next
	 */
	public String getNextAddress()
	{
	
	String nextAddress = "";
		String token = "";
		//accessing the next contact 's address that has not yet received the agent
	//	for(int i=0; i<this.contacts.size(); i++){
		//	if( this.available[i][0]==false ){
					nextAddress = this.contacts.elementAt(i).toString();
			//		break;
			//}
		//}
		
		Tokenizer tokField = new Tokenizer(nextAddress, ":");// colon needed to address format
		try{
			token = tokField.nextToken();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return token;
	}
	
	/**
	 * @return port is the port agent is to be sent to next
	 */
	public int getNextPort()
	{
	String nextAddress = "";
		String token = "";
		
		//get next contact 's address that has not yet received the agent
		for(int i=0; i<this.contacts.size(); i++){
			//if( this.available[i][0]==false ){
					nextAddress = this.contacts.elementAt(i).toString();
					System.out.println("nextAddress");
				//	break;
			//}
		}
		
		Tokenizer tokField = new Tokenizer(nextAddress, ":");
		try{
			token = tokField.nextToken();
			token = tokField.nextToken();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return Integer.parseInt(token);


	
}
